<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8"/>
		<title>
			Document
		</title>
		<script src="angular.js"></script>
	</head>
	<body>
		<table ng-app="myApp" ng-controller="myController">
			<tr>
				<td>
					用户注册:
				</td>
			</tr>
			<tr>
				<td>
					用户名:
					<input type="text" ng-model="userName"/>
				</td>
			</tr>
			<tr>
				<td>
					密码:
					<input type="password" ng-model="userPwd"/>
				</td>
			</tr>
			<tr>
				<td>
					确认密码:
					<input type="password" ng-model ="confirmPwd"/>
				</td>
			</tr>
			<tr>
				<td>
					同意注册协议:
					<input type="checkbox" ng-model="isAgree"/>
				</td>
			</tr>
			<tr>
				<td>
					<input type="submit" value="提交" ng-click="regediter()"/>
					<span>
						{{ nameHint }}
					</span>
				</td>
			</tr>
		</table>
		<script>
			var app = angular.module("myApp",[]);
			app.controller("myController",function($scope){
				var arr=JSON.parse(localStorage.getItem("UserData"))||[];
				$scope.regediter=function(){
					if($scope.userName.length<6){
							$scope.nameHint="用户名太短,请重新输入";
							return;
					}
					if($scope.userPwd.length<6){
						$scope.nameHint="密码太短,请重新输入";
						return;
					}else if($scope.confirmPwd!==$scope.userPwd){
						$scope.nameHint="两次输入的密码不一致,请重新输入";
						return;
					}
					if(!$scope.isAgree){
						$scope.nameHint="请勾选同意协议并继续";
						return;
					}
					var obj = {
						name:$scope.userName,
						pwd:$scope.userPwd
					};
					for(var k in arr){
						if(arr[k].name==obj.name){
								// console.log();
								$scope.nameHint="该用户名已存在!";
								return;
						}
					}
					arr.push(obj);
					localStorage.setItem("UserData",JSON.stringify(arr));
						var arr1 = JSON.parse(localStorage.getItem("UserData"));

					for(var k in arr1){
						if(arr1[k].name!==$scope.userName){
							continue;
						}else{
							$scope.nameHint="注册成功";
						}
					}
				};
			});
		</script>
	</body>
</html>
